﻿using System.Collections.Generic;
using System.Domain.Entity;
using System.Domain.IRepository;
using System.Linq;
using System.Repository.Base;
using System.Repository.DataBase.Models;
using Microsoft.EntityFrameworkCore;

namespace System.Repository
{
    public class SysRoleAuthorizeRepository : RepositoryDomain<SysRoleAuthorizeEntity, Sys_RoleAuthorize>, ISysRoleAuthorizeRepository
    {
        public SysRoleAuthorizeRepository(IDbContextConfig config) : base(config)
        {
        }

        public override bool Update(SysRoleAuthorizeEntity entity)
        {
            Sys_RoleAuthorize model = GetModel(entity.Id);
            if (model == null)
                return false;
            Dbcontext.Set<Sys_RoleAuthorize>().Attach(model);
            Dbcontext.Entry(model).State = EntityState.Unchanged;
            AutoCopy(model, entity);
            return Dbcontext.SaveChanges() > 0;
        }



        public List<SysRoleAuthorizeEntity> GetList(string roleId)
        {
            var expression = ExtLinq.True<Sys_RoleAuthorize>();
            expression = expression.And(t => t.RoleId == roleId);
            return GetList(expression);
        }

        public List<SysModuleEntity> GetMenuList(string roleId)
        {
            //SysModuleRepository moduleRep = new SysModuleRepository();
            //var expression = ExtLinq.True<Sys_RoleAuthorize>().And(t => t.RoleId == roleId).And(t => t.Type == 1);
            //List<SysRoleAuthorizeEntity> models = GetList(expression);
            //List<SysModuleEntity> moduleModels = new List<SysModuleEntity>();
            //foreach (var item in models)
            //{
            //    var moduleExp = ExtLinq.True<Sys_Module>().And(t => t.Id == item.ModuleId).And(t => t.EnabledMark == true);
            //    SysModuleEntity model = moduleRep.GetEntity(moduleExp);
            //    if (model != null)
            //        moduleModels.Add(model);
            //}
            //return moduleModels;

            SysModuleRepository moduleRep = new SysModuleRepository(ContextConfig);
            List<SysModuleEntity> moduleModels = new List<SysModuleEntity>();
            var mdata = (from m in Dbcontext.Sys_Module
                         join r in Dbcontext.Sys_RoleAuthorize on m.Id equals r.ModuleId into left
                         from r in left.DefaultIfEmpty()
                         where r.Type == 1 && m.EnabledMark == true && r.RoleId == roleId
                         select m);
            foreach (var data in mdata.ToList())
            {
                moduleModels.Add(moduleRep.AutoCopy(null, data));
            }
            return moduleModels;
        }

        public List<SysModuleAuthorizeEntity> GetButtonList(string roleId)
        {
            //SysModuleAuthorizeRepository moduleRep = new SysModuleAuthorizeRepository();
            //var expression = ExtLinq.True<Sys_RoleAuthorize>();
            //expression = expression.And(t => t.RoleId == roleId).And(t => t.Type == 2);
            //List<SysRoleAuthorizeEntity> models = GetList(expression);
            //List<SysModuleAuthorizeEntity> moduleModels = new List<SysModuleAuthorizeEntity>();
            //foreach (var item in models)
            //{
            //    //SysModuleAuthorizeEntity model = moduleRep.GetEntity(item.AuthorizeId);
            //    var moduleExp = ExtLinq.True<Sys_ModuleAuthorize>().And(t => t.Id == item.AuthorizeId).And(t => t.EnabledMark == true);
            //    SysModuleAuthorizeEntity model = moduleRep.GetEntity(moduleExp);
            //    if (model != null)
            //        moduleModels.Add(model);
            //}
            //return moduleModels;

            List<SysModuleAuthorizeEntity> moduleModels = new List<SysModuleAuthorizeEntity>();
            var mdata = (from m in Dbcontext.Sys_ModuleAuthorize
                         join r in Dbcontext.Sys_RoleAuthorize on m.Id equals r.AuthorizeId into left
                         from r in left.DefaultIfEmpty()
                         where r.Type == 2 && m.EnabledMark == true && r.RoleId == roleId
                         select m);
            SysModuleAuthorizeRepository moduleRep = new SysModuleAuthorizeRepository(ContextConfig);
            foreach (var data in mdata.ToList())
            {
                moduleModels.Add(moduleRep.AutoCopy(null, data));
            }
            return moduleModels;
        }

        public RoleAuthorizeEntity ActionValidate(string roleId, string url)
        {
            var mdata = (from r in Dbcontext.Sys_RoleAuthorize
                         join m in Dbcontext.Sys_Module on r.ModuleId equals m.Id into left
                         from m in left.DefaultIfEmpty()
                         where m.UrlAddress == url && m.EnabledMark == true && r.RoleId == roleId
                         select m);

            var amdata = (from r in Dbcontext.Sys_RoleAuthorize
                          join m in Dbcontext.Sys_ModuleAuthorize on r.AuthorizeId equals m.Id
                          where m.UrlAddress == url && m.EnabledMark == true && r.RoleId == roleId
                          select m);

            RoleAuthorizeEntity model = new RoleAuthorizeEntity();
            //return mdata.ToList().Count + amdata.ToList().Count > 0;
            if (mdata.ToList().Count > 0)
            {
                Sys_Module sysmodel = mdata.ToList().FirstOrDefault();
                if (sysmodel != null)
                {
                    model.ModuleId = sysmodel.Id;
                    model.Name = sysmodel.FullName;
                }
            }
            if (amdata.ToList().Count > 0)
            {
                Sys_ModuleAuthorize sysmodel = amdata.ToList().FirstOrDefault();
                if (sysmodel != null)
                {
                    model.ModuleId = sysmodel.ModuleId;
                    model.AuthorizeId = sysmodel.Id;
                    model.Name = sysmodel.FullName;
                }
            }
            return model;
            // return true;
        }
        
    }
}
